import http from './http'

export interface AttachUploadResponse {
  code: number
  msg: string
  data: {
    id: number //附件的id
    file_name: string
    file_size: number
    mime_type: string
    uploaded_at: string
  }
}

export interface AttachDeleteResponse {
  code: number
  msg: string
}

export interface AttachListResponse {
  code: number
  msg: string
  data: AttachListItem[]
}

// 上传附件（仅限保存的草稿内）
// export const uploadAttachmentAPI = async (
//   file: File,
//   email_id: number,
//   id: string,
//   token: string,
// ): Promise<AttachUploadResponse> => {
//   const formData = new FormData()
//   formData.append('file', file)
//   formData.append('email_id', String(email_id))
//   return await http.post('/api/attach/upload', formData, {
//     headers: {
//       id,
//       token,
//       'Content-Type': 'multipart/form-data',
//     },
//   })
// }

// export const uploadAttachmentAPI = async (
//   file: File,
//   email_id: number,
//   id: string,
//   token: string,
// )

// 上传附件（仅限保存的草稿内）
// export const uploadAttachmentAPI = async (
//   file: File,
//   email_id: number,
//   id: string,
//   token: string,
// ): Promise<AttachUploadResponse> => {
//   const formData = new FormData()
//   formData.append('file', file)
//   formData.append('email_id', email_id)
//   return await http.post('/api/attach/upload', formData, {
//     headers: {
//       id,
//       token,
//       // 不要写 'Content-Type': 'multipart/form-data'
//     },
//     // 不要加 contentType 相关配置
//   })
// }

// 下载附件（返回 Blob，可用于下载）
export const downloadAttachmentAPI = async (
  attachment_id: number,
): Promise<Blob> => {
  return await http.get(`attach/download?attachment_id=${attachment_id}`, {
    responseType: 'blob',
  })
}

// 删除附件（仅限草稿）
export const deleteAttachmentAPI = async (params: {
  attachment_id: number
}): Promise<AttachDeleteResponse> => {
  return await http.post('/attach/delete', params)
}

export interface AttachListItem {
  id: number
  file_name: string
  file_size: number
  mime_type: string
  uploaded_at: string
}

// 获取单邮件附件列表（必须已发送）
export const getAttachmentListAPI = async (params: {
  email_id: number
}): Promise<AttachListResponse> => {
  return await http.get('/attach/list', {
    params,
  })
}

// export const createDraftAPI = async (params: {
//   receiver: string
//   cc: string[]
//   subject: string
//   content: string
//   attachments: File[]
//   send_now: boolean
// }): Promise<DraftResponse> => {
//   return await http.post('/mail/draft', params)
// }
